package com.hollycrm.ahunicom.sheet.business.logic;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

import com.hollycrm.unicom.sheet.BeanResource;
import com.hollycrm.unicom.sheet.Constants;
import com.hollycrm.unicom.sheet.domain.DealSheet;
import com.hollycrm.unicom.sheet.monitor.MonitorSheetManager;
import com.hollycrm.unicom.sheet.monitor.SheetMonitorItem;
import com.hollycrm.springframework.factory.DefaultBeanFactory;
import com.hollycrm.unicom.sheet.flowsupport.ActionExecuteLogic;
import com.hollycrm.unicom.sheet.flowsupport.DoOperateContext;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
import org.springframework.orm.hibernate.HibernateTemplate;
import com.hollycrm.unicom.security.AuthToken;
import com.hollycrm.unicom.sheet.flow.SheetAction;

public class SheetBlackFlagLogic
    extends HibernateDaoSupport
    implements ActionExecuteLogic {

  public void execute(AuthToken authToken, SheetAction action,
                      DoOperateContext context) {
    HibernateTemplate ht = getHibernateTemplate();
    DealSheet dealSheet = (DealSheet) context.getOutputAttr(DealSheet.class);
    String operateType = context.getOutputAttr("operateType") == null ?
            "" : (String) context.getOutputAttr("operateType");
//----------------------------------------------------------------------
    String alertTime = dealSheet.getDealTime();
    String alertDesc = dealSheet.getDealContent();
//----------------------------------------------------------------------
    MonitorSheetManager mtr = (MonitorSheetManager) DefaultBeanFactory.getBean(
        BeanResource.SHEET_MONITOR_MANAGER);
    List ls = new ArrayList();
    try {
      ls = mtr.checkAlertInfo(dealSheet.getSheetFlowNO());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    SheetMonitorItem item = new SheetMonitorItem();
    if (ls != null && ls.size() > 1) {
      for (Iterator iter = ls.iterator(); iter.hasNext(); ) {
        SheetMonitorItem item1 = (SheetMonitorItem) iter.next();
        if (!Constants.SHEET_ALERT_FEEDBACK_REASON.equals(item1.getAlertReason()))
          item = item1;
      }
    }
    else if(ls != null && ls.size() == 1){
      item = (SheetMonitorItem)ls.get(0);
    }
      item.setAlertFlag(Constants.SHEET_ALERT_TYPE_MOST);
      item.setAlertReason(Constants.SHEET_ALERT_TIME_REASON);
      item.setAlertDesc(dealSheet.getDealContent());
      item.setSheetStage(Constants.SHEET_STAGE_END);
      item.setAlertCount(new Integer(item.getAlertCount().intValue() + 1));
      item.setAlertTime(alertTime);
      item.setAlertDesc(dealSheet.getDealContent());
      ht.update(item);
  }
}